package com.scalesoft.artsync.frontend.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import com.scalesoft.artsync.frontend.model.SubmissionTableModel;

public class FrontendViewImpl extends JFrame implements FrontendView {
	
	private static final long serialVersionUID = 4629248063190360022L;

	private JLabel labelImportFrom;
	private JLabel labelExportTo;
	private JComboBox comboImportPlugin;
	private JComboBox comboExportPlugin;
	private JButton buttonDoImport;
	private JButton buttonDoExport;
	private JButton buttonImportPluginSettings;
	private JButton buttonExportPluginSettings;
	private JButton buttonSelectAll;
	private JButton buttonSelectNone;
	private JButton buttonInvertSelection;
	private JButton buttonNewSubmission;
	private JTable submissionTable;
	private SubmissionTableModel submissionTableModel;

	private JMenuBar menuBar;
	private JMenu menuFile;
	private JMenuItem menuItemSettings;
	private JMenuItem menuItemQuit;

	
	public FrontendViewImpl() {

		menuBar = new JMenuBar();
		menuFile = new JMenu("File");
		menuItemSettings = new JMenuItem("Settings", KeyEvent.VK_S);
		menuFile.add(menuItemSettings);
		menuItemQuit = new JMenuItem("Quit", KeyEvent.VK_Q);
		menuFile.add(menuItemQuit);
		menuBar.add(menuFile);
		this.setJMenuBar(menuBar);
		
		labelImportFrom = new JLabel("Import from");
		labelExportTo = new JLabel("Export to");
		comboImportPlugin = new JComboBox();
		comboExportPlugin = new JComboBox();
		buttonDoImport = new JButton("Run Import");
		buttonImportPluginSettings = new JButton("Config");
		buttonDoExport = new JButton("Run Export");
		buttonExportPluginSettings = new JButton("Config");
		buttonSelectAll = new JButton("All");
		buttonSelectNone = new JButton("None");
		buttonInvertSelection = new JButton("Invert");
		buttonNewSubmission = new JButton("+New");
		submissionTableModel = new SubmissionTableModel();
		submissionTable = new JTable(submissionTableModel);
		submissionTable.getColumnModel().getColumn(0).setPreferredWidth(30);
		submissionTable.getColumnModel().getColumn(1).setPreferredWidth(104);
		submissionTable.getColumnModel().getColumn(0).setMaxWidth(30);
		submissionTable.getColumnModel().getColumn(1).setMaxWidth(104);
		submissionTable.setRowHeight(100);
		
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.gridx = 0;
		c.gridy = 0;
		panel.add(labelImportFrom, c);
		c.gridx++;
		panel.add(comboImportPlugin, c);
		c.gridx++;
		panel.add(buttonDoImport, c);
		c.gridx++;
		panel.add(buttonImportPluginSettings, c);
		c.gridx++;
		c.weightx = 1;
		panel.add(new JPanel(), c);
		c.gridx++;
		c.weightx = 0;
		panel.add(labelExportTo, c);
		c.gridx++;
		panel.add(comboExportPlugin, c);
		c.gridx++;
		panel.add(buttonDoExport, c);
		c.gridx++;
		panel.add(buttonExportPluginSettings, c);

		c.gridx = 0;
		c.gridy++;
		c.gridwidth = 9;
		c.weightx = 1;
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(buttonSelectAll);
		buttonPanel.add(buttonSelectNone);
		buttonPanel.add(buttonInvertSelection);
		buttonPanel.add(buttonNewSubmission);
		panel.add(buttonPanel, c);
		
		c.gridx = 0;
		c.gridy++;
		c.gridwidth = 9;
		c.weighty = 1;
		c.weightx = 1;
		c.fill = GridBagConstraints.BOTH;
		JScrollPane scrollPane = new JScrollPane(submissionTable);
		submissionTable.setFillsViewportHeight(true);
		panel.add(scrollPane, c);

		this.getContentPane().add(panel);
		this.pack();
		this.setExtendedState(MAXIMIZED_BOTH);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	@Override
	public void showWindow() {
		this.setVisible(true);
		this.toFront();
	}

	public JLabel getLabelImportFrom() {
		return labelImportFrom;
	}

	public JLabel getLabelExportTo() {
		return labelExportTo;
	}

	public JComboBox getComboImportPlugin() {
		return comboImportPlugin;
	}

	public JComboBox getComboExportPlugin() {
		return comboExportPlugin;
	}

	public JButton getButtonDoImport() {
		return buttonDoImport;
	}

	public JButton getButtonDoExport() {
		return buttonDoExport;
	}

	public JButton getButtonSelectAll() {
		return buttonSelectAll;
	}

	public JButton getButtonSelectNone() {
		return buttonSelectNone;
	}

	public JButton getButtonInvertSelection() {
		return buttonInvertSelection;
	}

	public JButton getButtonNewSubmission() {
		return buttonNewSubmission;
	}

	public JTable getSubmissionTable() {
		return submissionTable;
	}

	public SubmissionTableModel getSubmissionTableModel() {
		return submissionTableModel;
	}

	public JButton getButtonImportPluginSettings() {
		return buttonImportPluginSettings;
	}

	public JButton getButtonExportPluginSettings() {
		return buttonExportPluginSettings;
	}

}
